package com.gin.kotlin.no_gay_alliance.adapter.community

import android.content.Context
import android.view.View
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.activity.TopicListActivity
import com.gin.kotlin.no_gay_alliance.argument.TopicListArgument
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingRecyclerViewAdapter
import com.gin.kotlin.no_gay_alliance.base.GlideModule.Companion.CROSS_FADE
import com.gin.kotlin.no_gay_alliance.base.MyHandler
import com.gin.kotlin.no_gay_alliance.base.TextViewTarget
import com.gin.kotlin.no_gay_alliance.databinding.ItemForumSearchResultBinding
import com.gin.kotlin.no_gay_alliance.model.ForumEntryModel
import com.gin.kotlin.no_gay_alliance.model.ForumSearchResultModel
import com.gin.nga.enums.ForumType.COL
import com.gin.nga.enums.ForumType.FORUM

class ForumSearchResultAdapter(context: Context, handler: MyHandler) : AbstractBindingRecyclerViewAdapter<ItemForumSearchResultBinding, ForumSearchResultModel>(context, handler),
    View.OnClickListener {
    override fun onBindViewHolder(holder: ViewHolder<ItemForumSearchResultBinding>, position: Int, b: ItemForumSearchResultBinding, dataModel: ForumSearchResultModel) {
        val (entry, parent) = dataModel

        b.tvForum.text = entry.name
        // 图标大小（dp）
        val size = (64 * context.resources.displayMetrics.density).toInt()
        // 加载版面头像
        Glide.with(context)
            .load(entry.icon)
            .error(R.drawable.ic_forum_avatar)
            .transition(DrawableTransitionOptions.withCrossFade(CROSS_FADE))
            .into(TextViewTarget(b.tvForum, 0, size, size))

        if (parent != null) {
            b.tvForumParent.visibility = View.VISIBLE
            b.tvForumParent.text = parent.name
            // 图标大小（dp）
            val s = (26 * context.resources.displayMetrics.density).toInt()
            // 加载版面头像
            Glide.with(context)
                .load(parent.icon)
                .error(R.drawable.ic_forum_avatar)
                .transition(DrawableTransitionOptions.withCrossFade(CROSS_FADE))
                .into(TextViewTarget(b.tvForumParent, 2, s, s))
        } else {
            b.tvForumParent.visibility = View.GONE
        }


        b.tvForum.tag = entry
        b.tvForum.setOnClickListener(this)
        b.tvForumParent.tag = parent
        b.tvForumParent.setOnClickListener(this)


    }

    override fun onClick(v: View?) {
        val entry = v?.tag as? ForumEntryModel ?: return
        val type = when (entry.type) {
            COL -> TopicListArgument.Type.COL_LIST
            FORUM -> TopicListArgument.Type.FORUM_LIST
        }
        val forumId = if (entry.type == FORUM) entry.entryId else null
        val colTid = if (entry.type == COL) entry.entryId else null
        TopicListActivity.startActivity(context, TopicListArgument(type, forumId = forumId, colTid = colTid))
    }
}